/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import React, { useEffect, useState } from 'react';
import { Form, message, Card, List, Switch } from 'antd';
import { isFunction } from 'lodash';
import { setRiderSwitch } from '@/services/jdk/register';
import styles from './index.less';

const Index = ({ record: { is_self_delivery = '', user_id = '' } = {}, successCallBack }: any) => {
  const [formAuth] = Form.useForm();
  const [checked, setChecked] = useState<boolean>(false);

  const handleSwitch = async (checked_: boolean) => {
    const { code, msg } = await setRiderSwitch({
      user_id,
      is_self_delivery: checked_ ? '1' : 0,
    });
    if (code == 0) {
      if (isFunction(successCallBack)) {
        successCallBack();
      }
      message.success('操作成功');
    } else {
      message.error(msg);
    }
  };

  useEffect(() => {
    setChecked(`${is_self_delivery}` === '1');
  }, [is_self_delivery]);

  return (
    <Card title='其他关联权限设置模块' className={styles.drawerAuth} style={{ marginTop: 24 }}>
      <Form form={formAuth}>
        <List>
          <List.Item>
            <div>是否允许聚单客商家开通飓风跑腿权限</div>
            <div style={{ textAlign: 'right' }}>
              <Switch checked={checked} onChange={handleSwitch} />
            </div>
          </List.Item>
        </List>
      </Form>
    </Card>
  );
};

export default Index;
